package com.collection.admin.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.collection.common.base.BaseAction;
import com.collection.entity.BaseStrategy;
import com.collection.entity.CollectionModelToPublishModel;
import com.collection.entity.CollectionTask;
import com.collection.entity.ContentStrategy;
import com.collection.entity.PublishConfig;
import com.collection.entity.UrlParam;
import com.collection.entity.WebsiteStrategy;
import com.collection.service.CollectionTaskService;
import com.collection.service.ContentStrategyService;
import com.collection.service.PublishConfigService;
import com.collection.service.WebsiteStrategyService;

/**
 * 数据采集管理控制类
 * @author 冯勇
 */
@Controller
@Scope("prototype")
public class CollectionDataAction extends BaseAction {

	private static final long serialVersionUID = 685519596496856366L;

	@Autowired
	private CollectionTaskService collectionTaskService;
	
	@Autowired
	private WebsiteStrategyService websiteStrategyService;
	
	@Autowired
	private ContentStrategyService contentStrategyService;
	
	@Autowired
	private PublishConfigService publishConfigService;
	
	private CollectionTask collectionTask;
	
	private Integer taskId;
	
	private WebsiteStrategy websiteStrategy;
	
	private ContentStrategy contentStrategy;
	
	private PublishConfig publishConfig;
	
	private List<CollectionTask> collectionTasks;
	
	private List<BaseStrategy> baseStrategies;
	
	private List<CollectionModelToPublishModel> collectionModelToPublishModels;
	
	private List<UrlParam> urlParams;
	
	private boolean isCollectionWebsite;
	
	private boolean isCollectionContent;
	
	private boolean isPublishContent;
	
	private boolean isTest;
	
	/**
	 * 创建采集任务
	 * @return
	 */
	public String createCollectionTask() {
		if(collectionTask != null) {
			
			Integer id = collectionTask.getId();
			
			/*
			 * 如果ID不为空就作更新操作，否则作创建操作
			 */
			if(id != null) {
				collectionTaskService.modifyCollectionTask(collectionTask);
				
				/*
				 * 下一步加载采集任务的网址采集策略
				 */
				websiteStrategy = websiteStrategyService.getWebsiteStrategyByTaskId(id);
			} else {
				collectionTaskService.createCollectionTask(collectionTask);
				
				/*
				 * 如果taskId不为空则为复制编辑操作
				 */
				if(taskId != null) {
					websiteStrategy = websiteStrategyService.getWebsiteStrategyByTaskId(taskId);
					if(websiteStrategy != null) {
						websiteStrategy.setId(null);
						baseStrategies = websiteStrategy.getBaseStrategies();
						if(baseStrategies != null) {
							for(BaseStrategy bs:baseStrategies) {
								bs.setId(null);
							}
						}
						
						urlParams = websiteStrategy.getUrlParams();
						if(urlParams != null) {
							for(UrlParam up:urlParams) {
								up.setId(null);
							}
						}
					}
				}
			}
			
			if(websiteStrategy != null) {
				baseStrategies = websiteStrategy.getBaseStrategies();
				urlParams = websiteStrategy.getUrlParams();
			}
		}
		return "website";
	}
	
	/**
	 * 创建网址采集策略
	 * @return
	 */
	public String createWebsiteStrategy() {
		if(websiteStrategy != null && collectionTask != null) {
			Integer id = websiteStrategy.getId();
			
			collectionTask = collectionTaskService.getCollectionTaskById(collectionTask.getId());
			websiteStrategy.setCollectionTask(collectionTask);
			
			if(baseStrategies != null) {
				List<BaseStrategy> bss = new ArrayList<BaseStrategy>();
				for(BaseStrategy bs:baseStrategies) {
					if(bs != null) {
						bss.add(bs);
					}
				}
				websiteStrategy.setBaseStrategies(bss);
				
				/*
				 * 创建内容策略时应清空
				 */
				baseStrategies = null;
			}
			
			if(urlParams != null) {
				List<UrlParam> ups = new ArrayList<UrlParam>();
				for(UrlParam up:urlParams) {
					if(up != null) {
						ups.add(up);
					}
				}
				websiteStrategy.setUrlParams(ups);
			}
			
			/*
			 * id不为空作更新操作，否则作创建操作
			 */
			if(id != null) {
				websiteStrategyService.modifyWebsiteStrategy(websiteStrategy);
				
				/*
				 * 下一步加载采集任务的内容采集策略
				 */
				contentStrategy = contentStrategyService.getContentStrategyByTaskId(collectionTask.getId());
			} else {
				websiteStrategyService.createWebsiteStrategy(websiteStrategy);
				
				/*
				 * 如果taskId不为空则为复制编辑操作
				 */
				if(taskId != null) {
					contentStrategy = contentStrategyService.getContentStrategyByTaskId(taskId);
					if(contentStrategy != null) {
						contentStrategy.setId(null);
						baseStrategies = contentStrategy.getBaseStrategies();
						if(baseStrategies != null) {
							for(BaseStrategy bs:baseStrategies) {
								bs.setId(null);
							}
						}
					}
				}
			}
		
			if(contentStrategy != null) {
				baseStrategies = contentStrategy.getBaseStrategies();
			}
		}
		return "content";
	}
	
	/**
	 * 创建内容采集策略
	 * @return
	 */
	public String createContentStrategy() {
		if(contentStrategy != null) {
			Integer id = contentStrategy.getId();
			if(baseStrategies != null) {
				List<BaseStrategy> bss = new ArrayList<BaseStrategy>();
				for(BaseStrategy bs:baseStrategies) {
					if(bs != null) {
						bss.add(bs);
					}
				}
				contentStrategy.setBaseStrategies(bss);
			}
			
			collectionTask = collectionTaskService.getCollectionTaskById(collectionTask.getId());
			
			contentStrategy.setCollectionTask(collectionTask);
			
			/*
			 * id不为空作更新操作，否则作创建操作
			 */
			if(id != null) {
				contentStrategyService.modifyContentStrategy(contentStrategy);
				
				/*
				 * 下一步加载采集任务发布配置信息
				 */
				publishConfig = publishConfigService.getPublishConfigByTaskId(collectionTask.getId());
			} else {
				contentStrategyService.createContentStrategy(contentStrategy);
				
				/*
				 * 如果taskId不为空则为复制编辑操作
				 */
				if(taskId != null) {
					publishConfig = publishConfigService.getPublishConfigByTaskId(taskId);
					if(publishConfig != null) {
						publishConfig.setId(null);
						
						collectionModelToPublishModels = publishConfig.getCollectionModelToPublishModels();
						if(collectionModelToPublishModels != null) {
							for(CollectionModelToPublishModel cmtpm:collectionModelToPublishModels) {
								cmtpm.setId(null);
							}
						}
					}
				}
			}
			if(publishConfig != null) {
				collectionModelToPublishModels = publishConfig.getCollectionModelToPublishModels();
			}
			baseStrategies = contentStrategy.getBaseStrategies();
		}
		return "publish";
	}
	
	/**
	 * 创建任务采集发布配置信息
	 * @return
	 */
	public String createPublishConfig() {
		if(publishConfig != null) {
			Integer id = publishConfig.getId();
			
			collectionTask = collectionTaskService.getCollectionTaskById(collectionTask.getId());
			publishConfig.setCollectionTask(collectionTask);
			
			if(collectionModelToPublishModels != null) {
				List<CollectionModelToPublishModel> cmtps = new ArrayList<CollectionModelToPublishModel>();
				for(CollectionModelToPublishModel cmtp:collectionModelToPublishModels) {
					if(cmtp != null) {
						cmtps.add(cmtp);
					}
				}
				publishConfig.setCollectionModelToPublishModels(cmtps);
			}
			
			/*
			 * id不为空作更新操作，否则作创建操作
			 */
			if(id != null) {
				publishConfigService.modifyPublishConfig(publishConfig);
			} else {
				publishConfigService.createPublishConfig(publishConfig);
			}
		}
		return listCollectionTask();
	}
	
	/**
	 * 编辑采集任务
	 * @return
	 */
	public String editCollectionTask() {
		if(collectionTask != null) {
			collectionTask = collectionTaskService.getCollectionTaskById(collectionTask.getId());
			return "edit";
		} else {
			return listCollectionTask();
		}
	}
	
	/**
	 * 复制编辑采集任务
	 * @return
	 */
	public String copyEditCollectionTask() {
		if(collectionTask != null) {
			taskId = collectionTask.getId();
			collectionTask = collectionTaskService.getCollectionTaskById(taskId);
			
			/*
			 * 复制编辑时需要设置ID为空
			 */
			collectionTask.setId(null);
			return "edit";
		} else {
			return listCollectionTask();
		}
	}
	
	/**
	 * 删除采集任务
	 * @return
	 */
	public String deleteCollectionTask() {
		if(collectionTask != null) {
			collectionTaskService.deleteCollectionTask(collectionTask);
		} 
		return listCollectionTask();
	}
	
	/**
	 * 采集任务列表
	 * @return
	 */
	public String listCollectionTask() {
		collectionTasks = collectionTaskService.findAllCollectionTasks();
		return "manage";
	}
	
	/**
	 * ajax检测采集任务别名是否已存在
	 */
	public void ajaxCheckTaskAliasExist() {
		if(collectionTask != null) {
			String alias = collectionTask.getTaskAlias();
			getOut().print(collectionTaskService.taskAliasExist(alias));
		}
		
	}
	
	/**
	 * 采集数据
	 * @return
	 */
	public void collectionContent() {
		if(collectionTask != null) {
			Integer taskId = collectionTask.getId();
			collectionTask = collectionTaskService.getCollectionTaskById(taskId);
			
			if(collectionTask != null) {
				String tableName = collectionTask.getTaskAlias();
				
				websiteStrategy = websiteStrategyService.getWebsiteStrategyByTaskId(taskId);
				contentStrategy = contentStrategyService.getContentStrategyByTaskId(taskId);
				publishConfig = publishConfigService.getPublishConfigByTaskId(taskId);
				
				collectionTaskService.setTableName(tableName);
				
				collectionTaskService.setTest(isTest);
				
				if(isCollectionWebsite) {
					/*
					 * 采集网址
					 */
					collectionTaskService.collectionWebsite(websiteStrategy);
				}
		
				if(isCollectionContent) {
					/*
					 * 采集网址
					 */
					collectionTaskService.collectionContent(contentStrategy);
				}
				
				if(isPublishContent) {
					/*
					 * 发布内容
					 */
					collectionTaskService.publishContent(publishConfig);
				}
			}
		}
	}
	
	/**
	 * 发布采集到的内容
	 * @return
	 */
	public void publishContent() {
		if(collectionTask != null) {
			Integer taskId = collectionTask.getId();
			publishConfig = publishConfigService.getPublishConfigByTaskId(taskId);
			
			List<Map<String, Object>>  dataMaps = collectionTaskService.publishContent(publishConfig);
			
			if(dataMaps != null && dataMaps.size() > 0) {
				StringBuffer sb = new StringBuffer();
				for(Map<String, Object> dataMap:dataMaps) {
					for(Entry<String, Object> entry:dataMap.entrySet()) {
						sb.append(entry.getKey() + ":" + entry.getValue()+"\n\r");
					}
					getOut().println(sb);
					getOut().flush();
				}
			}
		}
	}

	public CollectionTask getCollectionTask() {
		return collectionTask;
	}

	public void setCollectionTask(CollectionTask collectionTask) {
		this.collectionTask = collectionTask;
	}

	public CollectionTaskService getCollectionTaskService() {
		return collectionTaskService;
	}

	public void setCollectionTaskService(CollectionTaskService collectionTaskService) {
		this.collectionTaskService = collectionTaskService;
	}
	
	public List<BaseStrategy> getBaseStrategies() {
		return baseStrategies;
	}

	public void setBaseStrategies(List<BaseStrategy> baseStrategies) {
		this.baseStrategies = baseStrategies;
	}

	public List<UrlParam> getUrlParams() {
		return urlParams;
	}

	public void setUrlParams(List<UrlParam> urlParams) {
		this.urlParams = urlParams;
	}

	public List<CollectionTask> getCollectionTasks() {
		return collectionTasks;
	}

	public WebsiteStrategy getWebsiteStrategy() {
		return websiteStrategy;
	}

	public void setWebsiteStrategy(WebsiteStrategy websiteStrategy) {
		this.websiteStrategy = websiteStrategy;
	}

	public ContentStrategy getContentStrategy() {
		return contentStrategy;
	}

	public void setContentStrategy(ContentStrategy contentStrategy) {
		this.contentStrategy = contentStrategy;
	}

	public PublishConfig getPublishConfig() {
		return publishConfig;
	}

	public void setPublishConfig(PublishConfig publishConfig) {
		this.publishConfig = publishConfig;
	}

	public List<CollectionModelToPublishModel> getCollectionModelToPublishModels() {
		return collectionModelToPublishModels;
	}

	public void setCollectionModelToPublishModels(
			List<CollectionModelToPublishModel> collectionModelToPublishModels) {
		this.collectionModelToPublishModels = collectionModelToPublishModels;
	}

	public Integer getTaskId() {
		return taskId;
	}

	public void setTaskId(Integer taskId) {
		this.taskId = taskId;
	}

	public boolean isCollectionWebsite() {
		return isCollectionWebsite;
	}

	public void setCollectionWebsite(boolean isCollectionWebsite) {
		this.isCollectionWebsite = isCollectionWebsite;
	}

	public boolean isCollectionContent() {
		return isCollectionContent;
	}

	public void setCollectionContent(boolean isCollectionContent) {
		this.isCollectionContent = isCollectionContent;
	}

	public boolean isPublishContent() {
		return isPublishContent;
	}

	public void setPublishContent(boolean isPublishContent) {
		this.isPublishContent = isPublishContent;
	}

	public boolean isTest() {
		return isTest;
	}

	public void setTest(boolean isTest) {
		this.isTest = isTest;
	}
}
